#pragma once

#include "menu.hpp"
#include "ui.hpp"
#include "ui_config.hpp"


/**
 * @brief 设备地址界面
 *
 */
class AddressScreen : public beef::InterfaceBase {
   private:
    MenuPage _menu_page{menu_items, MENU_ITEM_COUNT, MENU_ROW_COUNT};  // 菜单页面

    uint8_t _ret_code = 0;  // 返回值

    bool _device_loaded = false;  // 从机参数是否已经加载，如果没有加载，则禁止菜单滚动到从机参数部分
                                  // 每次修改从机地址时复位。
                                  // 在菜单界面，长按OK 强制刷新从机参数

    bool _cycle_up = false;
    bool _cycle_down = false;


    void reset_cycle() {
        _cycle_down = false;
        _cycle_up = false;
    }


    virtual void poll() override {
        bool send_buffer = false;

        bool right = button_right.clicked();
        if (right) {
            bkr.start_blink();
            send_buffer = true;
            reset_button_event();
            _ret_code = NAVIGATION_RIGHT;
            reset_cycle();
        }

        auto _prev_index = _menu_page.selected_index();

        if (button_up.clicked()) {
            // 当滚动到第一个菜单项时，按两次UP 可以滚动到最后一个菜单项
            if (_menu_page.is_top()) {
                if (_cycle_up) {
                    _menu_page.scroll_to_bottom();
                    _cycle_up = false;
                }
                else {
                    _cycle_up = true;
                }
            }
            else {
                _menu_page.scroll_up();
            }

            _cycle_down = false;
        }

        if (button_down.clicked()) {
            if (_menu_page.is_bottom()) {
                if (_cycle_down) {
                    _menu_page.scroll_to_top();
                    _cycle_down = false;
                }
                else {
                    _cycle_down = true;
                }
            }
            else {
                _menu_page.scroll_down();
            }

            _cycle_up = false;
        }

        if (_menu_page.selected_index() > MENU_INDEX_DEVICE_PARAM
            && !_device_loaded) {
            // 选中从机参数范围的菜单项时，调出对话框加载从机参数，
            // 如果加载失败，让菜单回退到之前的位置
            _menu_page.draw(false);
            u8g2.sendBuffer();

            EditScreen es{MENU_INDEX_LOAD, "从机参数"};
            auto r = enter(&es);
            if (r == NAVIGATION_RETURN_OK) {
                _device_loaded = true;
            }
            else {
                _menu_page.scroll_to(_prev_index);
            }
        }

        if (button_ok.clicked()) {
            EditScreen es{_menu_page.selected_index(), nullptr};
            enter(&es);

            if (_menu_page.selected_index() < SAVED_ITEM_COUNT) {
                if (_menu_page.selected_index() == MENU_INDEX_ADDRESS || _menu_page.selected_index() == MENU_INDEX_AUTO_ADDRESS) {
                    _device_loaded = false;
                }

                save_to_storage(menu_items, SAVED_ITEM_COUNT);
            }

            reset_cycle();
        }

        if (button_ok.long_pressed()) {
            EditScreen es{MENU_INDEX_LOAD, "从机参数"};
            auto r = enter(&es);
            if (r == NAVIGATION_RETURN_OK) {
                _device_loaded = true;
            }

            reset_cycle();
        }

        send_buffer = send_buffer || bkr.blink() || _menu_page.draw(false);

        if (send_buffer) {
            show_menu_index(_menu_page.selected_index(), _menu_page.item_count());
            u8g2.sendBuffer();
        }
    }


    virtual int show() override {
        while (1) {
            tick();

            if (_ret_code != 0) {
                // 返回值不为0，表示切换到其他界面
                return _ret_code;
            }

            wait_for(200);
        }
    }

    virtual void init(InterfaceBase *sub) override {
        reset_button_event();
        _ret_code = 0;

        set_response_timeout(MODBUS_CONFIG_TIMEOUT);
        u8g2.clearBuffer();
        bkr.stop_blink();
        put_slave_address();
        draw_right_arrow();
        draw_up_arrow();
        draw_down_arrow();

        // TODO: 读取保存的menu item 数据

        u8g2.setFont(CHS_FONT);
        _menu_page.draw(true);  // 初始绘制菜单项
        show_menu_index(_menu_page.selected_index(), _menu_page.item_count());
        u8g2.sendBuffer();
    }

   public:
    AddressScreen() {
        _menu_page.set_placing(15, 100, 8, 20);  // 设置菜单项的行高、宽度和偏移
    }
};